/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Entity.Category;
import Entity.Event;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Cyrax
 */
@Stateless
public class EventSessionBean implements EventSessionBeanLocal {

    @PersistenceContext(unitName = "EasyTickets-ejbPU")
    private EntityManager em;


    public void persist(Object object) {
        em.persist(object);
    }

    @Override
    public List<Event> ListAllEvent() {
        List<Event> ev = em.createNamedQuery("Event.findAll").getResultList();
        return ev;

   }
   @Override
   public void insertEvent(Event ev)
   {
       try {
           em.persist(ev);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
 
    @Override
    public void deleteEvent(Event event) {
        try {
            if (event != null) {
                em.remove(em.merge(event));
            } else {
                System.out.println("Not found event ID to delete");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<Event> searchEvent(String eventTitle) {
        List<Event> ent = null;

        ent = em.createQuery("SELECT e FROM Event e WHERE e.eventTitle like :eventTitle").setParameter("eventTitle", "%" + eventTitle + "%").getResultList();
        System.out.println("eventTitle : " + eventTitle);


        return ent;
    }
    @Override
    public void eventsUpdate(int eventId, int categoryId, String eventTitle, String eventInfo, String eventImg, Date startDate, int du, Date endDate) {
        Category cat=em.find(Category.class, categoryId);
      Event event = em.find(Event.class, eventId);
        if (event != null) {
           
            event.setCategory(cat);
            event.setEventTitle(eventTitle);
            event.setEventInfo(eventInfo);
            event.setEventImg(eventImg);
            event.setReleaseDate(startDate);
            event.setDuration(du);
            event.setEndDate(endDate);

            em.persist(em.merge(event));
        } else {
            System.out.println("Not found event ID to Update");
        }
    }
     boolean result = false;
     @Override
    public boolean checkEventTitle(String name)
    {
      List<Event> eList=null;
         eList=em.createQuery("SELECT e FROM Event e").getResultList();
         for(Event ev: eList)
         {
           if(ev.getEventTitle().equals(name))
           {
              result=true;
              break;
           }else
           {
               result=false;
           }
         }
        return  result;
    }


    @Override
    public List<Event> listbyCat(String catName) {
//        Category cat = (Category) em.createNamedQuery("Category.findByCategoryName").setParameter("categoryName", catName).getSingleResult();
        return em.createQuery("SELECT e FROM Event e WHERE e.category.categoryName = :categoryName").setParameter("categoryName", catName).getResultList();
    }

    @Override
    public List<Event> listAvailableEvent(){
//        Calendar cal = Calendar.getInstance();
//        Date now1 = cal.getTime();
        return em.createNativeQuery("sp_view_available_event",Event.class).getResultList();
    }

    @Override
    public List<Event> listNextEvent(){
        return em.createNativeQuery("sp_view_next_event",Event.class).getResultList();
    }









}
